/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package xowl.gmi.environment;

/**
 *
 * @author Laurent WOUTERS
 */
public class LoadingSplash extends javax.swing.JWindow {
    private static int width = 500;
    private static int height = 200;
    private static int border = 10;
    
    private LongJob job;
    private LongJobHandler handler;
    private javax.swing.JTextArea logArea;

    public LoadingSplash(Properties properties, LongJob job) {
        this.job = job;
        java.awt.Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screen.width-width)/2;
        int y = (screen.height-height)/2;
        setBounds(x, y, width, height);

        javax.swing.JLabel lblTitle = new javax.swing.JLabel(properties.title);
        lblTitle.setAlignmentY(javax.swing.JComponent.CENTER_ALIGNMENT);
        lblTitle.setForeground(java.awt.Color.WHITE);
        lblTitle.setFont(new java.awt.Font("Sans-serif", java.awt.Font.PLAIN, 22));
        javax.swing.JLabel lblVersion = new javax.swing.JLabel("v" + properties.version);
        lblVersion.setAlignmentY(javax.swing.JComponent.CENTER_ALIGNMENT);
        lblVersion.setForeground(java.awt.Color.WHITE);
        lblVersion.setFont(new java.awt.Font("Sans-serif", java.awt.Font.PLAIN, 10));
        
        javax.swing.JPanel rowTitle = new javax.swing.JPanel();
        rowTitle.setOpaque(false);
        rowTitle.setLayout(new javax.swing.BoxLayout(rowTitle, javax.swing.BoxLayout.X_AXIS));
        rowTitle.setAlignmentX(javax.swing.JComponent.CENTER_ALIGNMENT);
        rowTitle.add(javax.swing.Box.createRigidArea(new java.awt.Dimension(border, 0)));
        rowTitle.add(lblTitle);
        rowTitle.add(javax.swing.Box.createHorizontalGlue());
        rowTitle.add(lblVersion);
        rowTitle.add(javax.swing.Box.createRigidArea(new java.awt.Dimension(border, 0)));
        
        logArea = new javax.swing.JTextArea(job.getMessage());
        logArea.setFont(new java.awt.Font("Sans-serif", java.awt.Font.PLAIN, 12));
        logArea.setForeground(java.awt.Color.WHITE);
        logArea.setOpaque(false);
        logArea.setAlignmentX(javax.swing.JComponent.CENTER_ALIGNMENT);

        javax.swing.JLabel lblCopyright = new javax.swing.JLabel(properties.copyright);
        lblCopyright.setAlignmentX(javax.swing.JComponent.CENTER_ALIGNMENT);
        lblCopyright.setForeground(java.awt.Color.WHITE);
        lblCopyright.setFont(new java.awt.Font("Sans-serif", java.awt.Font.PLAIN, 10));
        javax.swing.JLabel logo = new javax.swing.JLabel(xowl.gmi.view.IconProvider.getEmbedded("eadsiw.png"));
        logo.setAlignmentX(javax.swing.JComponent.CENTER_ALIGNMENT);
        
        javax.swing.JPanel column = new javax.swing.JPanel();
        column.setOpaque(false);
        column.setLayout(new javax.swing.BoxLayout(column, javax.swing.BoxLayout.Y_AXIS));
        column.add(javax.swing.Box.createRigidArea(new java.awt.Dimension(0, border)));
        column.add(logo);
        column.add(javax.swing.Box.createRigidArea(new java.awt.Dimension(0, border)));
        column.add(rowTitle);
        column.add(javax.swing.Box.createRigidArea(new java.awt.Dimension(0, border)));
        column.add(logArea);
        column.add(javax.swing.Box.createRigidArea(new java.awt.Dimension(0, border)));
        column.add(lblCopyright);
        column.add(javax.swing.Box.createRigidArea(new java.awt.Dimension(0, border)));
        
        javax.swing.JPanel row = new javax.swing.JPanel() {
            @Override public void paintComponent(java.awt.Graphics g) { paint_background(g); }
        };
        row.setOpaque(false);
        row.setLayout(new javax.swing.BoxLayout(row, javax.swing.BoxLayout.X_AXIS));
        row.add(javax.swing.Box.createRigidArea(new java.awt.Dimension(10, 0)));
        row.add(column, java.awt.Component.CENTER_ALIGNMENT);
        row.add(javax.swing.Box.createRigidArea(new java.awt.Dimension(10, 0)));

        javax.swing.JPanel content = (javax.swing.JPanel)getContentPane();
        content.setOpaque(false);
        content.setLayout(new java.awt.BorderLayout());
        content.add(row, java.awt.BorderLayout.CENTER);
        
        this.handler = new LongJobHandler() {
            @Override public void output(final String message) {
                javax.swing.SwingUtilities.invokeLater(new java.lang.Runnable() {
                    @Override public void run() {
                        logArea.setText(logArea.getText() + message);
                        validate();
                        repaint();
                    }
                });
            }
            @Override public void setAdvance(float value) { }
        };
        
        this.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override public void windowOpened(java.awt.event.WindowEvent we) {
                java.lang.Runnable runnable = new java.lang.Runnable() {
                    @Override public void run() {
                        LoadingSplash.this.job.run(handler);
                        setVisible(false);
                        dispose();
                    }
                };
                java.lang.Thread thread = new java.lang.Thread(runnable, "XOWLGMIEnvironment Loader");
                thread.start();
            }
        });
    };
    
    private void paint_background(java.awt.Graphics g) {
        java.awt.Graphics2D g2d = (java.awt.Graphics2D)g.create();
        g2d.setPaint(java.awt.Color.BLACK);
        g2d.fill(new java.awt.Rectangle(0, 0, width, height));
    }
}
